package org.darkforest.merchant.controller;

import org.darkforest.dao.mapper.MemberMsgTplMapper;
import org.darkforest.model.MemberMsgTpl;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.ucenter.service.MessageService;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/4/28.
 */
@Controller
@RequestMapping(value = "notice")
public class NoticeController {


    @Autowired
    private MessageService messageService;


    @Autowired
    private MemberMsgTplMapper memberMsgTplMapper;


    @RequestMapping(value = "toNoticeSend")
    public ModelAndView toNoticeSend(){
        ModelAndView res = new ModelAndView("member/notice/notice");
        MemberMsgTpl memberMsgTpl =   new MemberMsgTpl(false);
        memberMsgTpl.setMmtType("other");
        List<MemberMsgTpl> memberMsgTplList = memberMsgTplMapper.findMemberMsgTplByConditon(memberMsgTpl);

        res.addObject("tplList",memberMsgTplList);
        return res;
    }

    @RequestMapping(value = "doNoticeSend")
    @AuthPassport
    public @ResponseBody boolean doNoticeSend(String messageBody,String mmtCode,String messageTitle,HttpServletRequest request){
        StoreSellerVo sellerVo = (StoreSellerVo) request.getSession().getAttribute("sellerInfo");
        messageService.sendSystemMessage(messageBody, sellerVo, mmtCode,messageTitle);
        return true;
    }


    @RequestMapping(value = "noticeModelList")
    public ModelAndView noticModelList(@RequestParam(required = false,defaultValue = "1") int p){
        ModelAndView res = new ModelAndView("member/notice/notice_model_list");

        MemberMsgTpl memberMsgTpl = new MemberMsgTpl();
        memberMsgTpl.setStart(p);
        List<MemberMsgTpl> memberMsgTplList = memberMsgTplMapper.findMemberMsgTplByConditon(memberMsgTpl);
        memberMsgTpl.setResultList(memberMsgTplList);
        res.addObject("pageBean", memberMsgTpl);
        return res;
    }


    @RequestMapping(value = "toAddNoticeModel")
    public ModelAndView toAddNoticeModel(){
        ModelAndView res = new ModelAndView("member/notice/notice_model_add");
        return res;
    }



    @RequestMapping(value = "doAddNoticeModel")
    public @ResponseBody boolean doAddNoticeModel(MemberMsgTpl memberMsgTpl){
        memberMsgTpl.setMmtType("member");
        memberMsgTplMapper.insert(memberMsgTpl);

        return true;
    }

    @RequestMapping(value = "toModifyNoticeModel")
    public ModelAndView toModifyNoticeModel(String mmtCode){
        ModelAndView res = new ModelAndView("member/notice/notice_model_edit");
        res.addObject("model", memberMsgTplMapper.selectByPrimaryKey(mmtCode));
        return  res;
    }


    @RequestMapping(value = "doModifyNoticeModel")
    public @ResponseBody boolean doModifyNoticeModel(MemberMsgTpl memberMsgTpl){
        memberMsgTplMapper.updateByPrimaryKeySelective(memberMsgTpl);
        return true;
    }


    @RequestMapping(value = "delNoticeMode")
    public @ResponseBody boolean delNoticeMode(String mmtCode){

        memberMsgTplMapper.deleteByPrimaryKey(mmtCode);

        return true;
    }




    @RequestMapping(value = "checkMmtCode")
    public @ResponseBody boolean checkMmtCode(String mmtCode){

        int i = memberMsgTplMapper.checkMmtCode(mmtCode);

        if(i>0){
            return false;
        }else{
            return true;
        }
    }

}
